package com.qcby.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qcby.Constants;
import com.qcby.model.Cinema;
import com.qcby.model.Hall;
import com.qcby.model.dto.CinemaToVo;
import com.qcby.model.dto.CinemaVo;
import com.qcby.model.resp.RespEntity;
import com.qcby.query.CinemaQuery;
import com.qcby.service.CinemaService;
import com.qcby.service.HallService;
import com.qcby.util.Page;

@RestController
@RequestMapping("/cinema")
public class CinemaController {
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private HallService hallService;
    @RequestMapping("/selectAll")
    public RespEntity<List<CinemaVo>> selectAll(){

        List<Cinema> cinemas=cinemaService.selectObjectAll();
        List<CinemaVo> cinemaVoList=new ArrayList<>(cinemas.size());

        for(Cinema cinema:cinemas){
            List<Hall> hallList = hallService.selectAllByCid(cinema.getcId());
            CinemaVo cinemaVo = new CinemaVo(cinema,hallList);
            cinemaVoList.add(cinemaVo);
        }
        return RespEntity.successCustomize(Constants.FLAG_200, "查询成功！", cinemaVoList);
    }
    @RequestMapping("/list")
    public RespEntity<CinemaToVo> searchCinema(@RequestBody CinemaQuery cq) {
        if(cq!=null){
            System.out.println(cq);
            if (cq.getPageNo() == null) {
                cq.setPageNo(1);
            }
            Page<Cinema> page = cinemaService.selectObjectByCondition(cq, "0");
            System.out.println(page);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", new CinemaToVo(page, cq));
        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"查询失败",null);
    }

    @RequestMapping("/addCinema")
    public RespEntity<String> addCinema(@RequestBody Cinema cinema) {
        if(cinema!=null){
            cinemaService.insert(cinema);
            return RespEntity.successCustomize(Constants.FLAG_200,"","添加成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"","添加失败，参数不能为空");
    }

    @RequestMapping("/deleteCinema")
    public RespEntity<String> deleteCinema(@RequestParam Integer cId) {
        if(cId!=null){
            cinemaService.deleteByPrimaryKey(cId);
            //放映厅中的对应的影院信息删除
            hallService.deleteByCid(cId);
            return RespEntity.successCustomize(Constants.FLAG_200,"","删除成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"","删除失败，参数不能为空");
    }

    @RequestMapping("/getCinemaById")
    public RespEntity<Cinema> getCinemaById(Integer cId){
        if(cId!=null){
            Cinema cinema=cinemaService.selectByPrimaryKey(cId);
            return RespEntity.successCustomize(Constants.FLAG_200,"",cinema);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"",null);
    }
    @RequestMapping("/updateCinema")
    public RespEntity<String> updateCinema(@RequestBody Cinema cinema) {
        if(cinema!=null){
            if(cinema.getcId()!=null){
                cinemaService.updateByPrimaryKeySelective(cinema);
                return RespEntity.successCustomize(Constants.FLAG_200,"","修改成功");
            }
        }
        return RespEntity.errorCustomize(Constants.FLAG_400,"","修改失败，参数不能为空");
    }

}
